package org.example.controller;

import org.example.model.category.CategoryBasic;
import org.example.model.common.dtos.ResponseResult;
import org.example.model.common.dtos.category.CategoryBasicDto;
import org.example.model.common.vos.category.SearchCategoryVo;
import org.example.service.CategoryBasicService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@CrossOrigin
@RestController
@RequestMapping("/category-basic")
public class CategoryBasicController {

    @Autowired
    private CategoryBasicService categoryBasicService;

    /**
     * 新增分类
     * @param categoryBasic
     * @return
     */
    @PostMapping("/category-basic")
    public ResponseResult AddCategoryBasic(@RequestBody CategoryBasic categoryBasic) {
        return ResponseResult.okResult(categoryBasicService.InsertCategoryBasic(categoryBasic));
    }

    /**
     * 根据id查询分类详情
     */
    @GetMapping("/category-basic/{id}")
    public ResponseResult GetCategoryBasicById(@PathVariable int id) {
        return ResponseResult.okResult(categoryBasicService.SelectCategoryById(id));
    }

    /**
     * 根据Id修改分类信息
     */
    @PutMapping("/category-basic")
    public ResponseResult UpdateCategoryBasicById(@RequestBody CategoryBasic categoryBasic) {
        return ResponseResult.okResult(categoryBasicService.UpdateCategoryBasicById(categoryBasic));
    }

    /**
     * 根据Id删除分类信息
     */
    @DeleteMapping("/category-basic/{id}")
    public ResponseResult DeleteCategoryBasicById(@PathVariable int id) {
        return ResponseResult.okResult(categoryBasicService.DeleteCategoryBasicById(id));
    }

    /**
     * 按条件(分类名称模糊查询，分类类型精确查询)分页查询
     */
    @GetMapping("/category-list/{pageIndex}/{pageSize}")
    public ResponseResult GetUsersByConditions(SearchCategoryVo searchCategoryVo, @PathVariable int pageIndex, @PathVariable int pageSize) {
        CategoryBasic categoryBasic = new CategoryBasic();
        BeanUtils.copyProperties(searchCategoryVo,categoryBasic);
        return ResponseResult.okResult(categoryBasicService.SelectPageByConditions(categoryBasic,pageIndex,pageSize));
    }
}
